package models

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	config2 "news-up/config"
)

var DB *gorm.DB

func Init() {
	var err error
	config := config2.AppConfig.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		config.User,
		config.Password,
		config.Host,
		config.Port,
		config.Database,
	)
	DB, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		fmt.Println("数据库链接失败")
	}
	err = DB.AutoMigrate(User{}, Art{})
	if err != nil {
		fmt.Println("迁移失败")
	}
}
